package com.euc.springbootframework.auth.manager;

import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

public class MyAccessDecisionManager implements AccessDecisionManager {

    //configAttribut 获取权限集合
    @Override
    public void decide(Authentication authentication, Object o, Collection<ConfigAttribute> configAttributes) throws AccessDeniedException, InsufficientAuthenticationException {
        Collection<? extends GrantedAuthority> myRoles = authentication.getAuthorities();
        for (GrantedAuthority myRole : myRoles) {
            for (ConfigAttribute urlRoles : configAttributes) {
                if (myRole.getAuthority().equals(urlRoles.getAttribute())) {
                    return;
                }
            }
        }
        throw new AccessDeniedException("Access Denied");
    }

    //if need filter
    @Override
    public boolean supports(ConfigAttribute configAttribute) {
        return true;
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return true;
    }
}
